﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace KnowledgeAssistant
{
    class AnswerUtil
    {
        public static List<Answer> GetAllAnswers(int question_id)
        {
            List<Answer> answers = new List<Answer>() { };

            DataTable answerstable = new DataTable();
            List<string> parameters = new List<string>() { question_id.ToString() };
            answerstable = DatabaseConnect.execSP("GetAllAnswers", parameters).Tables[0];

            Answer a = null;
            Question q = QuestionUtil.GetQuestion(question_id);

            foreach (DataRow r in answerstable.Rows)
            {
                a = new Answer((int)r["answer_id"], r["description"].ToString(), q, (bool)r["true"]);
            }

            return answers;
        }

        public static void UpdateAnswer(Answer a)
        {
            List<string> parameters = new List<string>();

            parameters.Add(a.AnswerID.ToString());
            parameters.Add(a.Description);
            parameters.Add((a.True)?"1":"0");
            
            DatabaseConnect.execSP("UpdateAnswer", parameters);
        }

        public static int AddAnswer(Answer a)
        {
            int answer_id = 0;
            List<string> parameters = new List<string>();
            parameters.Add(a.Question.QuestionID.ToString());
            parameters.Add(a.Description);
            parameters.Add((a.True) ? "1" : "0");
            answer_id = int.Parse(DatabaseConnect.execSP("AddAnswer", parameters).Tables[0].Rows[0][0].ToString());
            return answer_id;
        }

        public static void DeleteAnswer(Answer a)
        {            
            List<string> parameters = new List<string>();
            parameters.Add(a.AnswerID.ToString());
            DatabaseConnect.execSPNonQuery("DeleteAnswer", parameters);           
        }
    }
}
